package com.ccl.iot.device;import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.HashMap;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.Rect;
import android.view.MotionEvent;
import android.view.View;

import com.ccl.iot.publics.ClassManager;
import com.ccl.iot.IOTInterface.IOTAuthMonitorInterface;
import com.ccl.iot.IOTInterface.IOTAuthMonitorInterface.IOTAuthChangeListener;
import com.ccl.iot.IOTInterface.IOTOnlineMonitorInterface;
import com.ccl.iot.IOTInterface.IOTOnlineMonitorInterface.IOTOnlineStatusChangeListener;
import com.ccl.iot.IOTInterface.IOTStateMonitorInterface;
import com.ccl.iot.IOTInterface.IOTStateMonitorInterface.IOTStateChangeListener;

public abstract class DeviceControler  {
	/*
	 * 	Interfaces
	 */
	public interface OnBitmapChangeListener{
		public void onBitmapChange();
	}
	
	/*
	 * 	Static functions
	 */
	private static ArrayList<Class<?>> gControlerTypes = null;
	private static HashMap<IOTDevice, DeviceControler> gControlers = null;
	
	public static boolean RegisterControlerType(Class<?> aType){
		if (ClassManager.IsSubClass(DeviceControler.class, aType)){
			if (gControlerTypes == null){
				gControlerTypes = new ArrayList<Class<?>>();
			}else if (gControlerTypes.contains(aType)){
				return true;
			}
			
			return gControlerTypes.add(aType);
		}
		
		return false;
	}
	
	public static int GetControlerType(Class<?> aType){
		if (gControlerTypes != null){
			return gControlerTypes.indexOf(aType);
		}else{
			return -1;
		}
	}
	
	/**获取设备是哪种类型
	 * @param aDevice
	 * @return
	 */
	public static DeviceControler BuildControler(IOTDevice aDevice){
		if (aDevice != null){
			
			DeviceControler tControler = null;
			if (gControlers == null){
				gControlers = new HashMap<IOTDevice, DeviceControler>();
			}else{
				synchronized(gControlers){
					tControler = gControlers.get(aDevice);
				}
			}
			
			if (tControler == null){
				if (gControlerTypes != null){
					for (int i = 0, tCount = gControlerTypes.size(); i < tCount; i++){
						Class<?> tControlerType = gControlerTypes.get(i);
						
						if (tControlerType != null){
							Constructor<?> tConstructor;
							
							if ((tConstructor = ClassManager.FindConstructor(tControlerType, aDevice)) != null){
								try {
									tControler = (DeviceControler) tConstructor.newInstance(aDevice);
								} catch (Exception e) {
								}
								
								if (tControler != null){
									gControlers.put(aDevice, tControler);
									
									break;
								}
							}
						}							
					}
				}
			}
			
//			LogUtil.i("具体是什么设备：" + tControler.getClass().getName().toString() );
			
			return tControler;
		}
		
		return null;
	}
	
	/*
	 * 	Non-static functions
	 */
	private IOTDevice iDevice;
	private OnBitmapChangeListener iBitmapChangeListener = null;
	
	protected int iBackColor = Color.TRANSPARENT;
	protected int iWidth;
	protected int iHeight;
	
	protected View mParent;
	
	protected Rect iPadding = new Rect();
	
	private boolean iTouchDown = false;
	private Point iTouchDownPoint = null;
	
	public DeviceControler(IOTDevice aDevice){
		iDevice = aDevice;
	}
	
	public void setDevice(IOTDevice aDevice) {
		iDevice = aDevice;
	}
	
	public void setOnBitmapChangeListener(OnBitmapChangeListener aListener){
		iBitmapChangeListener = aListener;
		
		if (iDevice != null){
			if (iBitmapChangeListener != null){
				iDevice.addListener(iOnlineStatusListener);
				iDevice.addListener(iAuthChangeListener);
				iDevice.addListener(iStateListener);
			}else{
				iDevice.removeAllListeners(iOnlineStatusListener);
				iDevice.removeAllListeners(iAuthChangeListener);
				iDevice.removeAllListeners(iStateListener);
			}
		}
	}

	public void onBitmapChange(){
		if (iBitmapChangeListener != null){
			iBitmapChangeListener.onBitmapChange();
		}
	}
	
	private IOTOnlineStatusChangeListener iOnlineStatusListener = new IOTOnlineStatusChangeListener(){
		@Override
		public void onOnlineStatusChanged(IOTDevice aSource, boolean aOnline) {
			onBitmapChange();
		}
	};
	
	private IOTAuthChangeListener iAuthChangeListener = new IOTAuthChangeListener(){
		@Override
		public void onAuthChanged(IOTDevice aSource, int aNewAuth) {
			onBitmapChange();
		}
	};
	
	private IOTStateChangeListener iStateListener = new IOTStateChangeListener(){

		@Override
		public void onStateChanged(IOTStateMonitorInterface aSource, int aNewState) {
			onBitmapChange();
		}
	};
	
	public int getBackColor(){
		return iBackColor;
	}
	
	public void setBackColor(int aBackColor){
		if (aBackColor != iBackColor){
			iBackColor = aBackColor;
			
			onBitmapChange();
		}
	}
	
	public int getWidth(){
		return iWidth;
	}
	
	public int getHeight(){
		return iHeight;
	}
	
	public void resize(int aWidth, int aHeight){
		if (aWidth != iWidth || aHeight != iHeight){
			iWidth = aWidth;
			iHeight = aHeight;
			
			onResize(aWidth, aHeight);
		}
	}
	
	public Rect getPadding(){
		return iPadding;
	}
	
	public void setPadding(int aLeft, int aTop, int aRight, int aBottom){
		if (aLeft != iPadding.left || aTop != iPadding.top || aRight != iPadding.right || aBottom != iPadding.bottom){
			iPadding.set(aLeft, aTop, aRight, aBottom);
			onResize(iWidth, iHeight);
		}
	}
	
	protected void onResize(int aWidth, int aHeight){
		onBitmapChange();
	}
	
	public boolean isTouchDown(){
		return iTouchDown;
	}
	
	protected boolean onTouchDown(float aX, float aY){
		return false;
	}
	
	protected boolean onTouchMove(float aX, float aY){
		return false;
	}
	
	protected boolean onTouchUp(float aX, float aY){
		return false;
	}
	
	protected boolean onTouchCancel(float aX, float aY){
		return false;
	}
	
	public boolean processTouchEvent(MotionEvent aEvent){
		if (aEvent != null && iDevice != null && iDevice.isOnline()){
			float tX = aEvent.getX();
			float tY = aEvent.getY();
			
			switch (aEvent.getAction()){
				case MotionEvent.ACTION_DOWN:{
					iTouchDown = true;
					
					if (iTouchDownPoint == null){
						iTouchDownPoint = new Point((int)tX, (int)tY);
					}else{
						iTouchDownPoint.set((int)tX, (int)tY);
					}
					
					return onTouchDown(tX, tY);
				}
				case MotionEvent.ACTION_MOVE:{
					return onTouchMove(tX, tY);
				}
				case MotionEvent.ACTION_UP:{
					if (iTouchDown){
						iTouchDown = false;
						
						if (Math.abs(iTouchDownPoint.x - tX) < (iWidth >> 4) && Math.abs(iTouchDownPoint.y - tY) < (iHeight >> 4)){
							return onTouchUp(tX, tY);
						}
					}
					
					return onTouchCancel(tX, tY);
				}
			}
		}
	
	
		return false;
	}
	
	public Bitmap getIcon(int aWidth, int aHeight, boolean aPressing){
		return DeviceIconProvider.BuildBitmap(iDevice, DeviceIconProvider.KICON_STYLE_DEFAULT, aWidth, aHeight, iBackColor, aPressing);
	}
	
	public Bitmap getBitmap(int aWidth, int aHeight){
		return DeviceIconProvider.BuildBitmap(iDevice, DeviceIconProvider.KICON_STYLE_DEFAULT, aWidth, aHeight, iBackColor, false);
	}
	
	public Bitmap getBitmap(View parent){
		mParent = parent;
		return getBitmap(iWidth, iHeight);
	}

}
